/*******************************************************************************
* Copyright (C) 2014 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
*******************************************************************************
*/
/*============================================================================
 * 
 * Author(s): Maxim Integrated Products
 * Contributors:
 * Date:          
 * Purpose:       
 * Description:
 * 
 *==========================================================================*/
/** Global includes */
#include <errors.h>
/** Other includes */
#include <mml_sc.h>
/** Local includes */
#include <emv_config.h>
#include <emv_slot.h>
#include <emv_timer.h>



unsigned short const emv_timer_clocks[] =
{
    48000,
    24000,
    16000,
    12000,
    9600,
    8000,
    6860,
    6000,
    5330,
    4800, 
    4330,
    4000,
    3690,
    3430,
    3200,
    3000,
};

#define STATE_TIMER0_PENDING    

/******************************************************************************/
void emv_timer_handler(int param)
{
    emv_slot_t									*slot;


	slot = emv_get_slot();
	if ( ( slot ) &&
		( (unsigned int)slot != EMV_FAKE_ADD_PATTERN ) &&
		( slot->status & EMV_STATE_TIMER_PENDING ) )
	{
		mml_sc_ioctl(N_SCS_IOCTL_HALT_CLK_COUNTER, (void*)0);
		emv_cancel();
	}
	/** We're done */
	return;
}

/******************************************************************************/
int emv_timer_init(emv_slot_t *slot, int clock)
{     
    return mml_sc_attach( MML_SC_EVENT_CLOCK_COUNTER, emv_timer_handler);
}

/******************************************************************************/
int emv_timer_run(emv_slot_t *slot)
{
	int											result = COMMON_ERR_UNKNOWN;
	unsigned int								tmp = CONFIG_EMV_TIMER_DEFAULT;

	/**  */
	result = mml_sc_ioctl(N_SCS_IOCTL_LAUNCH_CLK_COUNTER, (void*)tmp);
	if ( result )
	{
		slot->status &= ~EMV_STATE_TIMER_PENDING;
	}
	else
	{
		slot->status |= EMV_STATE_TIMER_PENDING;
	}
	/** We're done */
    return result;
}

/******************************************************************************/
int emv_timer_halt(emv_slot_t *slot)
{
	int											result = COMMON_ERR_UNKNOWN;

	/**  */
    result = mml_sc_ioctl(N_SCS_IOCTL_HALT_CLK_COUNTER, (void*)0);
	if ( NO_ERROR == result )
	{
		slot->status &= ~EMV_STATE_TIMER_PENDING;
	}
	/** We're done */
    return result;
}

/******************************************************************************/
/* EOF */
